package org.jmhome.blog.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jmhome.blog.model.Board;
import org.jmhome.blog.model.BoardRecommandUser;
import org.jmhome.blog.model.BoardReply;
import org.jmhome.blog.service.AbstractJpaDaoService;
import org.jmhome.blog.service.BoardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("boardService")
public class BoardServiceImpl extends AbstractJpaDaoService implements
		BoardService {

	/**
	 * @see org.jmhome.blog.service.BoardService#insertBoard(org.jmhome.blog.model.Board)
	 */
	@Override
	public void insertBoard(Board board) {
		getJpaTemplate().persist(board);
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#updateBoard(org.jmhome.blog.model.Board)
	 */
	@Override
	public void updateBoard(Board board) {
		getJpaTemplate().merge(board);

	}

	/**
	 * @see org.jmhome.blog.service.BoardService#deleteBoard(org.jmhome.blog.model.Board)
	 */
	@Override
	public void deleteBoard(Board board) {
		getJpaTemplate().remove(board);

	}

	/**
	 * @see org.jmhome.blog.service.BoardService#deleteBoard(java.lang.Integer)
	 */
	@Override
	public void deleteBoard(Integer boardSeq) {
		Board board = getBoard(boardSeq);
		getJpaTemplate().remove(board);
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#getBoard(java.lang.Integer)
	 */
	@Override
	public Board getBoard(Integer boardSeq) {
		Board board = null;
		if (boardSeq == null) {
			board = new Board();
		} else {
			board = getJpaTemplate().find(Board.class, boardSeq);
		}
		return board;
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#getBoardReply(java.lang.Integer)
	 */
	@Override
	public BoardReply getBoardReply(Integer boardReplySeq) {
		BoardReply reply = getJpaTemplate().find(BoardReply.class,
				boardReplySeq);
		return reply;
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#insertBoardReply(org.jmhome.blog.model.BoardReply)
	 */
	@Override
	public void insertBoardReply(BoardReply reply) {
		getJpaTemplate().persist(reply);
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#updateBoardReply(org.jmhome.blog.model.BoardReply)
	 */
	@Override
	public void updateBoardReply(BoardReply reply) {
		getJpaTemplate().merge(reply);
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#deleteBoardReply(org.jmhome.blog.model.BoardReply)
	 */
	@Override
	public void deleteBoardReply(Integer boardReplySeq) {
		BoardReply reply = getBoardReply(boardReplySeq);
		getJpaTemplate().remove(reply);
	}

	/**
	 * @see org.jmhome.blog.service.BoardService#getBoardRecommandUser(java.lang.String,
	 *      org.jmhome.blog.model.Board)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public BoardRecommandUser getBoardRecommandUser(String userEmail,
			Integer boardSeq) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("recommand_board", boardSeq);
		params.put("recommandUserEmail", userEmail);

		List<BoardRecommandUser> recommandUser = getJpaTemplate()
				.findByNamedQueryAndNamedParams(
						"org.jmhome.blog.model.BoardRecommandUser", params);

		if (recommandUser != null && !recommandUser.isEmpty()) {
			return recommandUser.get(0);
		}

		return null;
	}

}
